<?php
/**
 * Created by PhpStorm.
 * User: 子不语
 * Date: 2016/6/16
 * Time: 17:52
 */
class BankingAction extends BaseAction
{
    function __construct()
    {
        $this->no_login=['reg','login'];
        parent::__construct();
    }
    function index(){
        $sql='select recv_cash_name from users where id='.$_SESSION['usersid'];
        $user_info=db::get_one($sql);
        $this->assign('recv_cash_name',$user_info->recv_cash_name);
        $sql="select * from withdrawals where uid=".$_SESSION['usersid'];
        $list=db::get_all($sql);
        foreach ($list as $k=>$v){
            $list[$k]->account=str_replace('/','',$v->account);
        }
        $this->assign('list',$list);
        $this->display('banking_index');
    }
    function bind(){
        $id=Req::get('id','intval')?:0;
        $sql='select * from withdrawals where id='.$id.' and uid='.$_SESSION['usersid'];
        $withdrawals=db::get_one($sql);
        if($withdrawals->type==3){
            $ex=explode('|',$withdrawals->address);
            $withdrawals->province=$ex[0];
            $withdrawals->city=$ex[1];
            $withdrawals->bank_name=$ex[2];
        }
        $this->assign('withdrawals',$withdrawals);
        $this->assign('id',$id);
        $this->display('banking_bind');
    }
    function add_account(){
        $type=Req::post('type');
        $id=Req::post('id','intval')?:0;
        if($type=='alipay' || $type=='weichat'){
            $acc=Req::post('acc');
            if($acc==''){
                $this->result(1,'账户不能为空');
                return;
            }
            if($type=='alipay'){
                $s_type=1;
            }elseif($type=='weichat'){
                $s_type=2;
            }
            $sql="select recv_cash_name from users where id=".$_SESSION['usersid'];
            $rename=db::get_one($sql,'assoc');
            if($rename==''){
                return $this->result(1,'请先去绑定付款人姓名' );
            }
            $sql='select id from withdrawals where uid='.$_SESSION['usersid'].' and type='.$s_type;
            $res=db::get_one($sql,'assoc');
            if($id>0){
                if($res['id']){
                    $sql="update withdrawals set account='$acc',add_time=now() where id=".$id;
                    db::_query($sql);
                    return $this->result(0,'修改成功');
                }
            }else{

                if($res)return $this->result(1,'您已经添加过了' );
                $sql="insert into withdrawals (uid,type,account,add_time) values(".$_SESSION['usersid'].",'$s_type','$acc',now())";
                db::_query($sql);
                return $this->result(0,'添加成功' );
            }
        }elseif ($type=='bank'){
            $acc=Req::post('acc');
            if($acc==''){
                $this->result(1,'账户不能为空');
                return;
            }
            $bank_type=Req::post('bank_type');
            if($type==''){
                $this->result(1,'请选择银行');
                return;
            }
            $province=Req::post('province');
            if($province==''){
                return $this->result(1,'开户行所在省份不能为空' );
            }
            $city=Req::post('city');
            if($city==''){
                return $this->result(1,'开户行所在市不能为空' );
            }
            $bank_name=Req::post('bank_name');
            if($bank_name==''){
                return $this->result(1,'开户行不能为空' );
            }
            $sql="select recv_cash_name from users where id=".$_SESSION['usersid'];
            $rename=db::get_one($sql,'assoc');
            if($rename==''){
                return $this->result(1,'请先去绑定付款人姓名' );
            }
            $sql='select id from withdrawals where uid='.$_SESSION['usersid'].' and type=3';
            $res=db::get_one($sql,'assoc');
            if($id>0){
                if($res['id']){
                    $sql="update withdrawals set name='$bank_type',account='$acc',address='".$province .'|'. $city.'|' . $bank_name."',add_time=now() where id=$id;";
                    db::_query($sql);
                    return $this->result(0,'修改成功');
                }
            }else {
                if ($res) {
                    return $this->result(1, '您已经添加过了');

                }
                $sql = "insert into withdrawals (uid,type,account,name,address,add_time) values(" . $_SESSION['usersid'] . ",3,'$acc','$bank_type','" . $province .'|'. $city.'|' . $bank_name . "',now())";
                db::_query($sql);
                return $this->result(0, '添加成功');
            }
        }
    }
    function withdrawals(){
        if(IS_AJAX)return $this->ajax_withdrawals();
        $sql='select id,type from withdrawals where uid='.$_SESSION['usersid'];
        $list=db::get_all($sql);
        $this->assign('list',$list);
        $this->RefreshPoints();
        $this->display('banking_withdrawals');
    }
    function ajax_withdrawals(){
        $pass =Req::post('pass');
        $money=Req::post('money');
        $select=Req::post('select','intval');
        if(empty($pass)) //密码空
        {
            return $this->result(1,'请输入安全密码！');
        }
        if($money<100){
            return $this->result(1,'单笔最小提款额：100.0 RMB');
        }
        $pass =$this->setPassword($pass);
        $isagent=$usersid=0;
        $sql = "select id,isagent from users where id = {$_SESSION['usersid']} and bankpwd='{$pass}' ";
            $row=db::get_one($sql,'assoc');
        if($row)
        {
            $usersid = $row["id"];
            $isagent = $row["isagent"];
            if($isagent==1){//是否代理
                $ip = get_ip();
                $sql="update agent set last_logintime=now(),last_loginip='{$ip}' where uid={$usersid}";
                db::_query($sql); //更新代理登录时间
            }
        }
        if($usersid ==0){
            return $this->result(1, '密码错误！');
        }
        $sql='select id,type,name,account,address from withdrawals where uid='.$_SESSION['usersid'].' and id='.$select;
        $account=db::get_one($sql,'assoc');
        //$count_account=has_account();
        if(!$account['id'])return $this->result(1,'请先添加收款账号!' );
        //$info=get_info();
        $i_money=floor($this->info['points']);
        if($money>$i_money){
            return $this->result(1, '您没有这么多的钱');
        }
        $this->RefreshPoints();
        $point=$money*1000;
        $ip=get_ip();
        db::_query("SET AUTOCOMMIT=0");
        db::_query("begin");
        $sql="insert into pay_online (order_id,uid,rmb,point,point_befor,point_after,add_time,pay_time,ip,state,cz_type,account,name,error_msg) values ('99','{$_SESSION['usersid']}','-$money','-$point',".$_SESSION['points'].",".($_SESSION['points']-$point).",now(),now(),'$ip','30','$account[id]','$account[account]','$account[name]','$account[address]')";
        if(!db::_query($sql)){
            db::_query("ROLLBACK");
        }
        $sql="update users set `points`=`points`-$point where id=".$_SESSION['usersid'];
        if(!db::_query($sql)){
            db::_query("rollback");
        }
        $sql="UPDATE centerbank SET score = score+$point WHERE bankIdx = 4";
        db::_query($sql);
        db::_query("commit");
        $this->withdrawals_log(11,$point,$_SESSION['points'],$_SESSION['bankpoints'],'提现申请');
        return $this->result(0, '您已提交申请,请耐心等待审核!');

    }
    function withdrawals_list(){
        $sql='select p.rmb,p.add_time,p.pay_time,p.state,w.type from pay_online p left join withdrawals w on w.uid=p.uid and p.cz_type=w.id where p.state in(30,31,32) and p.uid='.$_SESSION['usersid'].' order by p.id desc limit 20';
        $list=db::get_all($sql);
        foreach ($list as $k=>$v){
            $list[$k]->rmb=sprintf('%d',$v->rmb*-1);
            $list[$k]->type=$this->cz_type($v->type);
            $list[$k]->add_time=date('m-d',strtotime($v->add_time));
            $list[$k]->pay_time=date('m-d',strtotime($v->pay_time));
        }
        $this->assign('list',$list);
        $this->display('banking_withdrawals_list');
    }
    
}